package leetcode.editor.offer;

// https://leetcode.cn/problems/shan-chu-lian-biao-de-jie-dian-lcof/
class ShanChuLianBiaoDeJieDianLcof {
    public static void main(String[] args) {
        Solution solution = new ShanChuLianBiaoDeJieDianLcof().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)

    // Definition for singly-linked list.
    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    class Solution {
        /**
         * 虚拟头节点
         *
         * @param head
         * @param val
         * @return
         */
        public ListNode deleteNode(ListNode head, int val) {
            if (head == null) {
                return null;
            }
            ListNode dummy = new ListNode(-1);
            ListNode pre = dummy;
            pre.next = head;
            ListNode cur = pre.next;
            while (cur != null) {
                if (cur.val == val) {
                    pre.next = cur.next;
                } else {
                    pre = pre.next;
                }
                cur = cur.next;
            }
            return dummy.next;  // 返回虚拟头节点
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
